package com.stark.commons.spring.web.support.exception;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.netflix.hystrix.exception.HystrixBadRequestException;

/**
 * {@link HystrixBadRequestException} 异常处理器。
 * <p>返回状态码及错误信息。
 * @author Ben
 * @since 1.0.0
 * @version 1.0.0
 */
public class HystrixBadRequestExceptionHandler implements ExceptionHandler {

	@Override
	public boolean instanceofException(Exception ex) {
		return ex instanceof HystrixBadRequestException;
	}

	@Override
	public ResponseEntity<String> handle(Exception ex) {
		String errmsg = ((HystrixBadRequestException) ex).getMessage();
		String status = StringUtils.substringBefore(errmsg, ":");
		String message = StringUtils.substringAfter(errmsg, ":");
		return ResponseEntity.status(HttpStatus.valueOf(Integer.valueOf(status))).body(message);
	}

}
